package com.imooc.service.impl;

import com.imooc.mapper.StuMapper;
import com.imooc.pojo.Stu;
import com.imooc.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2020/1/28-2020
 * @Description: stuservice 对应的实现
 */
@Service
public class StuServiceImpl implements StuService {

    @Autowired
    private StuMapper stuMapper;

    @Transactional(propagation = Propagation.SUPPORTS)  //查询仅需要事务的支持
    @Override
    public Stu getStuInfo(Integer id) {
        Stu stu = stuMapper.selectByPrimaryKey(id);
        return stu;
    }

    @Transactional(propagation = Propagation.REQUIRED) //保存操作需要在一个事务中进行
    @Override
    public void saveStu() {
        Stu stu=new Stu();
        stu.setAge(20);
        stu.setName("xiaoming");
        stuMapper.insert(stu);

    }

    @Transactional(propagation = Propagation.REQUIRED) //更新操作需要在一个事务中进行
    @Override
    public void updateStu(Integer id) {
        Stu stu=new Stu();
        stu.setId(id);
        stu.setAge(20);
        stu.setName("jack");
        //根据主键进行更新
        stuMapper.updateByPrimaryKey(stu);
    }

    @Transactional(propagation = Propagation.REQUIRED) //删除操作需要在一个事务中进行
    @Override
    public void deleteStu(Integer id) {
        stuMapper.deleteByPrimaryKey(id);
    }

}
